
#include <BFile>
#include <cstdio>
#include "member_BLibrary.h"

using namespace BWE;

member_BLibrary::member_BLibrary()
{
	handle = 0;
}
member_BLibrary::~member_BLibrary()
{

}

bool member_BLibrary::load()
{
	if (fileName.size())
	{
#ifdef _LINUX_
		handle = dlopen(fileName.cstr(), RTLD_NOW);
		char* error = dlerror();
		perror(error);
#else
		bool ret = BFile::Exists(fileName);
		handle = ::LoadLibraryA(fileName.cstr());
		DWORD error = GetLastError();
#endif
	}
	return handle != 0;
}
bool member_BLibrary::unload()
{
	if(handle)
	{
#ifdef _LINUX_
		int ret = dlclose(handle);
		return ret == 0;
#else
		bool ret = (bool)FreeLibrary(handle);
		return ret;
#endif
		handle = 0;
	}
	return false;
}
void* member_BLibrary::address(const char* name)
{
	if(handle)
	{
#ifdef _LINUX_
		return (void*)dlsym(handle, name);
#else
		return (void*)GetProcAddress(handle, name);
#endif
	}
	return 0;
}
